package com.lynch.mp3;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.lynch.downlod.HttpDownloader;
import com.lynch.model.Mp3Info;
import com.lynch.mp3.service.DownloadService;
import com.lynch.xml.Mp3ListContentHandler;

public class Mp3ListActivity extends ListActivity {
	
	/**�����int����*/
	private static final int Update = 1;
	private static final int About = 2;
	//Ϊʲô������127.0.0.1�Ͳ���
	private static final String urlStrOffice = "http://172.16.141.239:8080/mp3/resources.xml";
	//jiali
	private static final String urlStrHome = "http://192.168.1.5:8080/mp3/resources.xml";
	
	/**mp3�б�*/
	private List<Mp3Info> mp3List = null;
																	
	/**
	 * ���û����menu��ť֮�󣬻���ø÷��������ǿ�������������м����Լ��İ�ť�ؼ�
	 * �������2����ť
	 */
	public boolean onCreateOptionsMenu(Menu menu) {
		
		//��ť �ڶ�����id������������򣬵��ĸ��Ǳ���
		menu.add(0, Update, 1, R.string.mp3ListUpdate);
		menu.add(0, About, 2, R.string.mp3ListAbout);
		return super.onCreateOptionsMenu(menu);
	}

	/**
	 * ���menu��ť��ÿһ������ᴥ���������
	 */
	public boolean onOptionsItemSelected(MenuItem item) {
		
		if(item.getItemId()==Update){
			//�û�����˸����б?ť
			updateListView();
		}else if(item.getItemId()==About){
			//�û�����˹��ڰ�ť
		}
		return super.onOptionsItemSelected(item);
	}
	
	//����xml�ļ�������ȡ���ַ�
	private String downloadXML(String urlStr){
		
		HttpDownloader httpDownloader = new HttpDownloader();
		String result = httpDownloader.download(urlStr);
		return result;
	}
	
	//����xml
	private List<Mp3Info> parse(String xmlStr){
		
		SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
		List<Mp3Info> mp3List = new ArrayList<Mp3Info>();
		try {
			XMLReader xmlReader = saxParserFactory.newSAXParser().getXMLReader();
			Mp3ListContentHandler mp3ListContentHandler = new Mp3ListContentHandler(mp3List);
			xmlReader.setContentHandler(mp3ListContentHandler);
			xmlReader.parse(new InputSource(new StringReader(xmlStr)));
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return mp3List;
	}
	
	/**
	 * ����mp3�б� 
	 */
	private void updateListView() {
		
		//���ذ�����mp3����Ϣ��xml�ļ�
		String xml = downloadXML(urlStrOffice);
		//��xml�ļ����н��������������Ľ����õ�Mp3Info�����У�������Щ�Է��ŵ�List����
		mp3List = parse(xml);
		//����SimpleAdapter
		SimpleAdapter simpleAdapter = buildSimpleAdapter(mp3List);
		//�����SimpleAdapter�������õ�ListActivity����
		setListAdapter(simpleAdapter);
	}
	
	/**
	 * ����SimpleAdapter
	 */
	private SimpleAdapter buildSimpleAdapter(List<Mp3Info> mp3List){
		
		//���һ��list���󣬲����յı�׼����MP3Info���е������ӵ�List����ȥ
		List<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
		Iterator<Mp3Info> iterator = mp3List.iterator();
		while (iterator.hasNext()) {
			Mp3Info mp3Info = iterator.next();
			HashMap<String,String> map = new HashMap<String,String>();
			map.put("mp3Name", mp3Info.getMp3Name());
			map.put("mp3Size", mp3Info.getMp3Size());
			list.add(map);
		}
		//����һ��SimpleAdapter����
		SimpleAdapter simpleAdapter = new SimpleAdapter(this,list,R.layout.mp3infoitem,new String[]{"mp3Name","mp3Size"},new int[]{R.id.mp3Name,R.id.mp3Size});
		return simpleAdapter;
	}
	
	/**
	 * ÿ���һ���б�ѡ��ͻ������к���
	 */
	protected void onListItemClick(ListView l, View v, int position, long id) {

		//����û�����б?�е�λ�����õ���Ӧ�Ķ���
		Mp3Info mp3Info = mp3List.get(position);
		//���intent����
		Intent intent = new Intent();
		//��mp3�浽intent����
		intent.putExtra("mp3Info", mp3Info);
		intent.setClass(this, DownloadService.class);
		//��������
		startService(intent);
		super.onListItemClick(l, v, position, id);
	}

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.remotemp3list);
        updateListView();
    }
}